using System;
using System.Collections.Generic;
using System.Text;
using Evolution.Engine.GUI.Components;
using Evolution.Engine.Objects;
using Evolution.Engine;
using Microsoft.Xna.Framework;
using Evolution.Engine.Managers;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Evolution.GUI.Components
{
    public delegate void ButtonClick(object sender);
    public class Pushbutton : Control
    {
        public static Pushbutton CreateCloseButton(string name, string text, Vector2 location, Vector2 size, Form parent)
        {
            return new Pushbutton(
                name,
                text,
                "Content/GUI/Button/CloseButton/ClosePressed",
                "Content/GUI/Button/CloseButton/CloseNormal",
                parent,
                location,
                size
                );
        }

        protected Texture2d _PushedImage = null;
        protected Texture2d _NormalImage = null;
        protected ButtonState _State = ButtonState.Released;
        protected Color _FontColor;
        protected string _Font;

        public event ButtonClick OnClick;
        public string Font { get { return _Font; } set { _Font = value; } }
        public Color FontColor { get { return _FontColor; } set { _FontColor = value; } }

        public Pushbutton(string Name, string Text, Form parent, Vector2 position, Vector2 size)
            : base(parent)
        {
            _Name = Name;
            _Text = Text;
            _Font = "Default";
            _FontColor = Color.Yellow;
            _Location = position;
            _Size = new SizeF(size.X, size.Y);
            _Enable = true;
            _State = ButtonState.Released;
            _Visible = true;

            _PushedImage = new Texture2d(
                new Vector2(_Location.X, _Location.Y),
                new Vector2(_Size.Width, _Size.Height),
                "Content/GUI/Button/Pressed"
            );
            _PushedImage.LoadContent();

            _NormalImage = new Texture2d(
                new Vector2(_Location.X, _Location.Y),
                new Vector2(_Size.Width, _Size.Height),
                "Content/GUI/Button/Normal"
            );
            _NormalImage.LoadContent();
        }

        public Pushbutton(string Name, string Text, string PushedAsset, string NormalAsset, Form parent, Vector2 position, Vector2 size)
            : base(parent)
        {
            _Name = Name;
            _Text = Text;
            _Font = "Default";
            _FontColor = Color.Yellow;
            _Location = position;
            _Size = new SizeF(size.X, size.Y);

            if (!string.IsNullOrEmpty(PushedAsset))
            {
                _PushedImage = new Texture2d(new Vector2(_Location.X, _Location.Y), new Vector2(_Size.Width, _Size.Height), PushedAsset);
                _PushedImage.LoadContent();
            }

            if (!string.IsNullOrEmpty(NormalAsset))
            {
                _NormalImage = new Texture2d(new Vector2(_Location.X, _Location.Y), new Vector2(_Size.Width, _Size.Height), NormalAsset);
                _NormalImage.LoadContent();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            Texture2d tmp = null;
            switch (_State)
            {
                case ButtonState.Released:
                    tmp = _NormalImage;
                    break;

                case ButtonState.Pressed:
                    tmp = _PushedImage;
                    break;
            }

            tmp.Position = new Vector2(Bounds.Left, Bounds.Top);
            tmp.Draw(gameTime);
            tmp.Position = _Location;

            if (_Text.Length > 0)
            {
                Vector2 pos = FontManager.Instance[_Font].MeasureString(_Text);

                if (_State == ButtonState.Pressed)
                {
                    pos.Y += 2;
                    pos.X += 2;
                }

                pos.X = (this._Size.Width / 2) - (pos.X / 2) + Bounds.Left;
                pos.Y = (this._Size.Height / 2) - (pos.Y / 2) + Bounds.Top + 1;

                if (Bounds.Contains((int)InputManager.Instance.CurrentMouseLocation.X, (int)InputManager.Instance.CurrentMouseLocation.Y) )
                    TextManager.Instance.Write(_Font, _Text, pos, Color.Black);
                else
                    TextManager.Instance.Write(_Font, _Text, pos, _FontColor);
            }
        }
        public override void OnMouseDown(MouseEventArgs e)
        {
            if (Bounds.Contains((int)InputManager.Instance.CurrentMouseLocation.X, (int)InputManager.Instance.CurrentMouseLocation.Y) &&
                e.Button == MouseButton.Left)
            {
                _State = ButtonState.Pressed;
            }
        }
        public override void OnMouseUp(MouseEventArgs e)
        {
            if (Bounds.Contains((int)InputManager.Instance.CurrentMouseLocation.X, (int)InputManager.Instance.CurrentMouseLocation.Y) &&
                e.Button == MouseButton.Left)
            {
                if (OnClick != null)
                {
                    OnClick(this);
                    e.Handled = true;
                }

                _Parent.SetActiveControl(this);
            }

            _State = ButtonState.Released;
        }
        public override void SetLocation()
        {
            _NormalImage.Position = _Location;
            _PushedImage.Position = _Location;
        }
        public override void Resize()
        {
            _NormalImage.Size = new Vector2(_Size.Width, _Size.Height);
            _PushedImage.Size = new Vector2(_Size.Width, _Size.Height);
        }
    }
}
